<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="备品备件名称">
              <a-input placeholder="请输入备品备件名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="备品备件编号">
              <a-input placeholder="请输入备品备件编号" v-model="queryParam.number"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
<!--      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <a-button type="primary" icon="download" @click="handleExportXls('view_inventory_sum')">导出</a-button>
<!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
<!--      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap1"
        @change="handleTableChange">
<!--        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"-->
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="name" slot-scope="text,record">
          <a @click="handleDetailDrawer(record)">{{text}}</a>
        </template>
        <span slot="overdue" slot-scope="text, record">
        <div style="color:red">{{text}}</div>
        </span>

        <span slot="action" slot-scope="text, record">
<!--          <a @click="handleEdit(record)">采购申请</a>

          <a-divider type="vertical" />-->
          <a @click="chuKu(record)">出库</a>

            <a-divider type="vertical" />
          <a @click="yiKu(record)">移库</a>
        </span>

      </a-table>
    </div>
    <TabsDrawer @changeTabs="changeTabs" :width="1200" ref="tabsDrawer">
          <span  slot="wsnub" slot-scope="{text,record}" :style="{color:record.securityNub<=record.wsnub?'#6B778C':'red'}">{{text}}</span>
      <template slot="tabs-0">
        <collapse-comp @initData="initData" ref="collapsRef"></collapse-comp>
      </template>
      <template slot="tabs-1">
        <div>
          <a-input-search  v-model="queryParam.purchaseNub"  placeholder="采购单号" style="width: 200px;margin-bottom: 20px" @search="onSearch"/>
          <a-table
            :loading="drawerLoading"
            class="caigou"
            :rowKey="id"
            :columns="caiguoColumns"
            :dataSource="tabDataSource"
            :pagination="ipagination"
            @change="handleTableChange"
          >
          </a-table>
        </div>
      </template>
      <template slot="tabs-2">
        <div>
          <a-input-search v-model="searchValue"  placeholder="采购单号/仓库" style="width: 200px;margin-bottom: 20px" @search="onSearch"/>
          <a-table
            :scroll="{x: 1200}"
            :loading="drawerLoading"
            class="ruku"
            rowKey="id"
            :columns="tabColumns"
            :dataSource="tabDataSource"
            :pagination="ipagination"
            @change="handleTableChange"
          >
          </a-table>
        </div>
      </template>
      <template slot="tabs-3">
        <div>
          <a-input-search v-model="searchValue" placeholder="采购单号/仓库" style="width: 200px;margin-bottom: 20px" @search="onSearch"/>
          <a-table
            :scroll="{x: 1200}"
            :loading="drawerLoading"
            class="ruku"
            rowKey="id"
            :columns="tabColumns"
            :dataSource="tabDataSource"
            :pagination="ipagination"
            @change="handleTableChange"
          >
          </a-table>
        </div>
      </template>
      <template slot="tabs-4">
        <div>
          <a-input-search v-model="searchValue" placeholder="采购单号/仓库" style="width: 200px;margin-bottom: 20px" @search="onSearch"/>
          <a-table
            :scroll="{x: 1200}"
            :loading="drawerLoading"
            class="chuku"
            rowKey="id"
            :columns="tabColumns"
            :dataSource="tabDataSource"
            :pagination="ipagination"
            @change="handleTableChange"
          >
          </a-table>
        </div>
      </template>
      <template slot="tabs-5">
        <div>
<!--          whname-->
          <a-input-search  v-model="searchValue"  placeholder="采购单号/采购主题" style="width: 200px;margin-bottom: 20px" @search="onSearch"/>
          <a-table
            :scroll="{x: 1200}"
            :loading="drawerLoading"
            class="yiku"
            rowKey="id"
            :columns="tabColumns"
            :dataSource="tabDataSource"
            :pagination="ipagination"
            @change="handleTableChange"
          >
          </a-table>
        </div>
      </template>
    </TabsDrawer>
  <ene-outbound-modal ref="modalForm" @ok="modalFormOk"></ene-outbound-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import TabsDrawer from '@/components/Drawer/TabsDrawer'
  import {getAction} from "@api/manage";
  import CollapseComp from '@/components/Collapse/CollapsComp'
  import EneOutboundModal from './modules/EneOutboundModal'
  const  caiguoColumns=[
    {
      title: '采购单号',
      align: "center",
      dataIndex: 'purchaseNub',
      scopedSlots: {customRender: 'purchaseNub'},
    },
    {
      title: '采购名称',
      align: "center",
      dataIndex: 'name'
    },
    {
      title: '采购数量',
      align: "center",
      dataIndex: 'enePurchasingSpareparts.quantity'
    },
    {
      title: '采购部门',
      align: "center",
      dataIndex: 'procurementDepartment_dictText'
    },
    {
      title: '采购人',
      align: "center",
      dataIndex: 'purchasingPerson_dictText'
    },
    {
      title: '采购日期',
      align: "center",
      dataIndex: 'purchaseDate',
      customRender: function (text) {
        return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
      }
    },
    {
      title: '实时库存',
      align: "center",
      dataIndex: 'wsnub'
    },
  ]
  export default {
    name: 'ViewInventorySumList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      TabsDrawer,
      CollapseComp,
      EneOutboundModal
    },
    data () {
      return {
        searchValue:'',
        description: 'view_inventory_sum管理页面',
        // 采购批次
        /* 分页参数 */
        ipagination:{
          current: 1,
          pageSize: 10,
          total:0,
        },
        tabDataSource:[],
        queryParam:{
          pageNo:1,
          pageSize: 10
        },
        tabColumns:[],
        caiguoColumns,
        drawerLoading:false,
        selectedTabs:'caigou',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'备品备件名称',
            align:"center",
            dataIndex: 'name',
            scopedSlots: { customRender: 'name' }
          },
          {
            title:'备品备件编号',
            align:"center",
            dataIndex: 'number'
          },
          {
            title:'规格型号',
            align:"center",
            dataIndex: 'model'
          },
          {
            title:'寿命时长（天）',
            align:"center",
            dataIndex: 'lifetime'
          },
          {
            title:'安全库存数量',
            align:"center",
            dataIndex: 'securityNub'
          },
          {
            title:'最高库存上限',
            align:"center",
            dataIndex: 'maxNub'
          },
          {
            title:'使用年限',
            align:"center",
            dataIndex: 'serviceLife'
          },
          {
            title:'单位',
            align:"center",
            dataIndex: 'unit'
          },
          {
            title:'货期（天）',
            align:"center",
            dataIndex: 'deliveryTime'
          },
          {
            title:'用途',
            align:"center",
            dataIndex: 'purpose'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remarks'
          },
          {
            title:'实时库存',
            align:"center",
            dataIndex: 'wsnub'
          },
/*          {
            title:'是否有过期备件',
            align:"center",
            dataIndex: 'overdue',
            scopedSlots: { customRender: 'overdue' }
          },*/
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/ene/viewInventoryCollectionSum/list",
          exportXlsUrl: "/ene/viewInventoryCollectionSum/exportXls",
          caigou:"/ene/enePurchasingManagement/batch",
          ruku:"/online/cgreport/api/getColumnsAndData/1793174404903772161",
          chuku:"/online/cgreport/api/getColumnsAndData/1793479470709760001",
          yiku:"/online/cgreport/api/getColumnsAndData/1793481422667538433"

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {

    },
    methods: {
      onSearch(value){
        console.log("search",value)
        this.queryParam.pageNo=1
        if(this.selectedTabs=="ruku"){
          this.queryParam.superQueryMatchType='or'
          this.queryParam.superQueryParams=JSON.stringify([{"rule":"like","type":"string","dictCode":"","extendOption":["Y","N"],"val":this.searchValue,"field":"whname"},{"rule":"like","type":"string","dictCode":"","extendOption":["Y","N"],"val":this.searchValue,"field":"purchase_nub"}])
        }
        this.getTabData(this.selectedTabs)
      },
      chuKu(record) {
        this.$refs.modalForm.check(record);
        this.$refs.modalForm.title = "出库";
        this.$refs.modalForm.disableSubmit = false;
      },
      yiKu(record) {
        this.$refs.modalForm.check(record);
        this.$refs.modalForm.title = "移库";
        this.$refs.modalForm.disableSubmit = false;
      },
      handleDetailDrawer(record) {
        this.$refs.tabsDrawer.record = record;
        this.$refs.tabsDrawer.topData = [
          {
            name: '备品备件编号', value: record.number, lg: 3
          },
          {
            name: '规格型号', value: record.model, lg: 3
          },
          {
            name: '安全库存数量', value: record.securityNub, lg: 3
          },
          {
            name: '最高库存上限', value: record.maxNub, lg: 3
          },
          {
            name: '单位', value: record.unit, lg: 3
          },
          {
            name: '寿命时长（天）', value: record.lifetime, lg: 3
          },
          {
            name: '实时库存', value: record.wsnub, lg: 2,key:'wsnub'
          }

        ];
        this.$refs.tabsDrawer.tabsData = [
          {
            title: '基础信息'
          },
          {
            title: '采购批次'
          },
          {
            title: '入库信息'
          },
          {
            title: '出库信息'
          },
          {
            title: '移库信息'
          },
        ]
        this.selectedRecord = record

        this.$refs.tabsDrawer.visible = true

      },
      initData() {
        const record = this.selectedRecord
        this.$refs.collapsRef.collapseData = [
          {
            title: '基础信息',
            children: [
              {
                label: '备品备件名称',
                value: record.name
              },
              {
                label: '备品备件编号',
                value: record.number
              },
              {
                label: '规格型号',
                value: record.model
              },
              {
                label: '寿命时长（天）',
                value: record.lifetime
              },
              {
                label: '安全库存数量',
                value: record.securityNub
              },
              {
                label: '最高库存数量',
                value: record.maxNub
              },
              {
                label: '单位',
                value: record.unit
              },
              {
                label: '用途',
                value: record.purpose
              },
              {
                label: '备注',
                value: record.remarks
              }
            ]
          },

        ]
      },
      getTabData(name){
        this.drawerLoading=true
        getAction(this.url[name],this.queryParam).then(res => {
          if(res.success){
            if(this.selectedTabs!="caigou"){
              this.tabColumns = res.result.columns.map(e=>(Object.assign(e,{width:120})))
              this.tabDataSource = res.result.data.records
              this.ipagination.total=res.result.data.total
            }else{
              this.tabDataSource=res.result.records
              this.ipagination.total=res.result.total
            }





          }

        }).finally(()=>{
          this.drawerLoading=false
        })
      },
      changeTabs(value) {
        this.queryParam={
          pageNo:1,
          pageSize: 10
        }
        this.tabColumns=[]
        this.tabDataSource=[]
        if (value == '1') {

          this.queryParam.id=this.selectedRecord.id
          this.selectedTabs="caigou"
          this.getTabData("caigou")
        }
        if(value == '2'){

          this.queryParam.self_spid=this.selectedRecord.id
          this.selectedTabs="ruku"
          this.getTabData("ruku")

        }
        if(value == '3'){

          this.queryParam.self_spid=this.selectedRecord.id
          this.selectedTabs="chuku"
          this.getTabData("chuku")
        }
        if(value == '4'){


          this.queryParam.self_spid=this.selectedRecord.id
          this.selectedTabs="yiku"
          this.getTabData("yiku")

        }
      },
      handleTableChange(pagination, filters, sorter) {
        //分页、排序、筛选变化时触发
        this.queryParam.pageNo=pagination.current
        this.ipagination = pagination;
        this.getTabData(this.selectedTabs);
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'name',text:'备品备件名称',dictCode:''})
        fieldList.push({type:'string',value:'number',text:'备品备件编号',dictCode:''})
        fieldList.push({type:'string',value:'model',text:'规格型号',dictCode:''})
        fieldList.push({type:'double',value:'lifetime',text:'寿命时长（天）',dictCode:''})
        fieldList.push({type:'double',value:'securityNub',text:'安全库存数量',dictCode:''})
        fieldList.push({type:'double',value:'maxNub',text:'最高库存上限',dictCode:''})
        fieldList.push({type:'string',value:'serviceLife',text:'使用年限',dictCode:''})
        fieldList.push({type:'string',value:'unit',text:'单位',dictCode:''})
        fieldList.push({type:'double',value:'deliveryTime',text:'货期（天）',dictCode:''})
        fieldList.push({type:'string',value:'purpose',text:'用途',dictCode:''})
        fieldList.push({type:'string',value:'remarks',text:'备注',dictCode:''})
        fieldList.push({type:'string',value:'warehouseLocationId',text:'库位管理ID',dictCode:''})
        fieldList.push({type:'string',value:'location',text:'入库库位',dictCode:''})
        fieldList.push({type:'double',value:'wsnub',text:'实时库存',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>